package com.ts.api.http.feedback;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.id.IdUtils;
import com.gitee.apanlh.util.log.LogLevel;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpResponse;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpMethod;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.http.weixin.WeiXin;
import com.ts.api.module.channel.entity.vo.ReqAnXinFenQiVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 腾讯广告
 *
 * @author caoxin
 */
@Component
public class TengXunGuangGao3FeedbackHandler implements FeedbackHandler {

    @Override
    public boolean match(RequestVO requestVO) {
        return Eq.str(requestVO.getStr("type"), "tengxunguanggao.3");
    }

    @Override
    public Object feedback(RequestVO requestVO) {
        String code = requestVO.getStr("code");
        if (ValidParam.isNotEmpty(code)) {
            String openid = this.weixin.getOpenid(WEIXIN_APPID, WEIXIN_SECRET, code);
            if (ValidParam.isNotEmpty(openid)) {
                // RESERVATION 表单预约
                this.trace(openid, WEIXIN_APPID, "RESERVATION");
                ReqAnXinFenQiVO form = requestVO.get("form", ReqAnXinFenQiVO.class);
                // 当芝麻分为 700分以上
                if(ValidParam.isNotNull(form) && Eq.str(form.getSesameCredit(), "4")) {
                    // CREDIT 授信
                    this.trace(openid, WEIXIN_APPID, "CREDIT");
                }
            }
        }
        return new HashMap<>();
    }

    @Autowired
    private WeiXin weixin;

    private static final String WEIXIN_APPID = "wx32e65022385329af";
    private static final String WEIXIN_SECRET = "01414983580affde827397821270900d";

    /**
     * 密钥信息，可在 DataNexus 的 数据源 - 【查看密钥】获取
     */
    private static final String ACCESS_TOKEN = "c590e692b2164fef1e9e8c8e25ed4657";

    private void trace(String openid, String appid, String actionType) {
        String timestamp = String.valueOf((int) (System.currentTimeMillis() / 1000));
        String nonce = IdUtils.getSimpleUuid();

        Map<String, Object> bodyMap = MapUtils.newHashMap();
        bodyMap.put("account_id", 58317528);
        bodyMap.put("user_action_set_id", 1215209542);
        bodyMap.put("actions", CollUtils.newArrayList(actionsList -> {
            actionsList.add(MapUtils.newHashMap(actionsMap -> {
                actionsMap.put("action_time", (int) (System.currentTimeMillis() / 1000));
                actionsMap.put("user_id", MapUtils.newHashMap(userIdMap -> {
                    userIdMap.put("wechat_openid", openid);
                    userIdMap.put("wechat_app_id", appid);
                }));
                actionsMap.put("action_type", actionType);
            }));
        }));

        String reqJson = JSONObject.toJSONString(bodyMap);

        final String api_url = "https://api.e.qq.com/v1.3/user_actions/add";
        String url = String.format("%s?access_token=%s&timestamp=%s&nonce=%s", api_url, ACCESS_TOKEN, timestamp, nonce);
        HttpResponse build = HttpClientBuilder.builder(url, HttpMethod.POST)
                .withBodyJson(reqJson)
                .withHeader((headerMap) -> {
                    headerMap.put("Content-Type", "application/json");
                })
                .withLogLevel(LogLevel.WARN)
                .withErrorLogLevel(LogLevel.WARN)
                .build();
        String str = build.getStr();
    }
}
